<template>
  <el-form :model="item" ref="moForm" :rules="rules" label-width="130px">
    <el-form-item prop="cardName" label="	置顶卡小程序展示名">
      <el-input
        v-model.trim="item.cardName"
        placeholder="请输入名称"
      ></el-input>
    </el-form-item>
    <el-form-item prop="cardDesc" label="	置顶卡微信购买名">
      <el-input
        v-model.trim="item.cardDesc"
        placeholder="请输入置顶卡微信购买名"
      ></el-input>
    </el-form-item>
    <el-form-item prop="cardTime" label="置顶时间">
      <el-input
        v-model.trim="item.cardTime"
        placeholder="请输入置顶时间"
      ></el-input>
    </el-form-item>
    <el-form-item prop="price" label="购买价格">
      <el-input
        v-model.trim="item.price"
        placeholder="请输入购买价格"
      ></el-input>
    </el-form-item>
    <el-form-item prop="jifen" label="兑换条件">
      <el-input
        v-model.trim="item.jifen"
        placeholder="请输入兑换条件"
      ></el-input>
    </el-form-item>
    <el-form-item prop="imageFile" label="图片">
      <el-button class="setimg" type="primary" @click="setImg"
        >选择图片</el-button
      >
      <br />
      <span class="imginfo">请选择.jpg/.png格式图片，大小不超过1M</span>
      <input
        type="file"
        accept=".jpg, .png"
        id="fileInput"
        @change="imgChange($event)"
      />
      <div class="imgbox">
        <img :src="iconImage" id="image" />
      </div>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleSubmitClick()">提交</el-button>
      <el-button @click="handleBackClick">返回</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  props: ["item"],
  data() {
    return {
      reader: null,
      imgBase: "",
      iconImage: "",
      cates: [],
      rules: {
        cardName: [
          { required: true, trigger: "blur", message: "置顶卡小程序展示名" },
        ],
        cardDesc: [
          { required: true, trigger: "blur", message: "置顶卡微信购买名" },
        ],
        cardTime: [{ required: true, trigger: "blur", message: "置顶时间" }],
        price: [{ required: true, trigger: "blur", message: "购买价格" }],
        jifen: [{ required: true, trigger: "blur", message: "兑换条件" }],
        // seat: [{ required: true, message: "请选择位置", trigger: "blur" }],
        imageFile: [{ required: true, message: "请选择图片", trigger: "blur" }],
        // link: [
        //   { required: true, message: "请选择活动链接", trigger: "blur" },
        //   { type: "url", message: "请输入正确格式的链接", trigger: "blur" },
        // ],
        // endTime: [
        //   {
        //     validator: check_date,
        //     message: "请选择合法的开始结束时间",
        //     trigger: "blur",
        //   },
        // ],
      },
    };
  },
  computed: {
    obj() {
      return {
        cardName: this.item.cardName,
        cardDesc: this.item.cardDesc,
        cardTime: this.item.cardTime,
        price: this.item.price,
        jifen: this.item.jifen,
        image: this.item.image,
        imageFile: this.item.imageFile,
      };
    },
  },
  watch: {},
  created() {
    this.reader = new FileReader();
    this.reader.onload = (e) => {
      this.imgBase = e.target.result;
    };
    setTimeout(() => {
      if (this.item.image) {
        this.iconImage = this.item.image;
      }
    }, 500);
  },
  methods: {
    handleBackClick() {
      this.$router.go(-1);
    },
    handleSubmitClick() {
      this.$emit("doSubmit");
    },
    valid(callback) {
      this.$refs["moForm"].validate((valid) => {
        callback(valid);
      });
    },
    setImg() {
      const target = document.getElementById("fileInput");
      target && target.click();
    },
    imgChange(e) {
      console.log(e);
      const img = e.target.files[0];
      if (img.size / 1024 < 2 * 1024) {
        this.reader.readAsDataURL(img);
        this.item.imageFile = img;
        setTimeout(() => {
          const pos = this.imgBase.indexOf("base64") + 7;
          this.imgBase = this.imgBase.substr(pos);
          this.iconImage = "data:image/png;base64," + this.imgBase;
        }, 500);
      } else {
        this.$message.error("图片大小不可超过2M");
      }
      this.$refs["moForm"].validateField("imageFile");
    },
  },
};
</script>

<style scoped>
#fileInput {
  width: 0;
  height: 0;
  overflow: hidden;
}

.setimg {
  float: left;
  margin-left: 0;
}

.imgbox {
  border: 1px solid #eee;
  width: 200px;
  height: auto;
}

.imgbox img {
  align: center;
  max-width: 100%;
}

.imginfo {
  font-size: 12px;
  color: #666;
}
</style>
